package com.jk11a.shangdaast.user;



import java.util.List;

import com.jk11a.shangdaast.R;
import com.jk11a.shangdaast.dao.SubjectDao;
import com.jk11a.shangdaast.dao.UserDao;
import com.jk11a.shangdaast.dbopenhelper.DBDao;
import com.jk11a.shangdaast.notice.Notice;
import com.jk11a.shangdaast.subject.SubjectBean;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	static String YES = "yes";
	static String NO = "no";
	private EditText mUser;
	private EditText mPassword;
	private Button loginButton;
	private Button registButton;
	private SharedPreferences sp=null;
	private String FILE = "saveUserNamePwd";
	private static int USERID;
	String name;
	String password;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		mUser = (EditText) findViewById(R.id.login_user_edit);
		mPassword = (EditText) findViewById(R.id.login_passwd_edit);
		loginButton = (Button) findViewById(R.id.login_login_btn);
		registButton = (Button) findViewById(R.id.login_regist_btn);
		
		//打开Preferences，名称为saveUserNamePwd，如果存在则打开它，否则创建新的Preferences
		sp = this.getSharedPreferences(FILE, Context.MODE_PRIVATE);
	
		registButton.setOnClickListener(new OnClickListener() {		
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(LoginActivity.this, RegistActivity.class);
				startActivity(intent);
			}
		});
		loginButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {	
		
				name=mUser.getText().toString();
				password=mPassword.getText().toString();
				new LoginThread(LoginActivity.this).start();			
			}

		});
	}
	
	private class LoginThread extends Thread {
		private LoginActivity login2;

		public LoginThread(LoginActivity login1) {
			login2 = login1;
		}
		
		@Override
		public void run() {
			UserDao userDao=new UserDao();
			USERID=userDao.login(name, password);
			if(name.equals("1")&&password.equals("1"))           //测试号，不联网直接登录
				USERID=1;
			if(USERID==0){
				login2.handler.sendEmptyMessage(1);   //登陆失败
			}else{
				login2.handler.sendEmptyMessage(0);
				
			
			}
		}

	}

	public void login_back(View v) {
		this.finish();
	}
	
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				//将输入的用户名密码写入SharedPreferences
				SharedPreferences.Editor editor = sp.edit();
				editor.putInt("id", USERID);
				Log.i("userid",Integer.toString(USERID));
				editor.putString("name", mUser.getText().toString());
				editor.putString("password", mPassword.getText().toString());
				editor.commit();
				Intent intent = new Intent();
				intent.setClass(LoginActivity.this, Notice.class);
				startActivity(intent);
				finish();
				Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_LONG)
				.show();
		break;
			case 1:
				Intent intent2 = new Intent();
				intent2.setClass(LoginActivity.this, LoginActivity.class);
				startActivity(intent2);
				finish();
				Toast.makeText(LoginActivity.this, "用户名或密码不正确", Toast.LENGTH_LONG)
						.show();
				break;

			default:
				break;
			}
		}

	};
}
